<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\UploadFile;
use App\Http\Requests\InsertClassRequest;
use App\Models\Category;
use App\Models\Classes;
use App\Models\Course;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ClassController extends Controller
{
    use UploadFile;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
//        分类id
        $category_id = $request->get('category_id');
//        课程id
        $course_id = $request->get('course_id');
//        课时名称
        $name = $request->get('name');
        //分类
        $cates = Category::all(['id', 'name']);
//        课时列表
        $classes = Classes::with(['course', 'category']);
        $category_id && $classes = $classes->where('category_id', $category_id);
        $course_id && $classes = $classes->where('course_id', $course_id);
        $name && $classes = $classes->where('name', 'like', $name . '%');
        $classes = $classes->paginate();
//        追加参数
        $classes = $classes->appends($request->only(['category_id', 'course_id', 'name' => $name]));
        return view('admin.class.index', compact('cates', 'classes'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //分类
        $cates = Category::all(['id', 'name']);
        return view('admin.class.create', compact('cates'));
    }

    /**
     * 添加处理
     * @param InsertClassRequest $request
     * @return array
     */
    public function store(InsertClassRequest $request)
    {
        $data = $request->all();
        $data['cover'] = '';
        if ($request->hasFile('file')) {
//            上传成功，调用上传方法
            $fileinfo = $this->up($request->file('file'));
            $fileinfo && $data['cover'] = '/uploads/' . $fileinfo->getFilename();
        }
        $res = Classes::create($data);
        if ($res) return ['status' => 0, 'msg' => '添加成功', 'url' => $data['cover']];
        return ['status' => 1, 'msg' => '添加失败'];
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //分类
        $cates = Category::get(['id', 'name']);
        //课时信息
        $class = Classes::find($id);
        //根据课时的分类id获取课程列表
        $courses = Course::where('category_id', $class->category_id)->get(['id', 'name']);
        return view('admin.class.edit', compact('cates', 'class', 'courses'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param InsertClassRequest $request
     * @param  int $id
     * @return array
     */
    public function update(InsertClassRequest $request, $id)
    {
        $data = $request->only(['name','course_id','category_id','price','url','cover']);
        if ($request->hasFile('file')) {
//            上传成功，调用上传方法
            $fileinfo = $this->up($request->file('file'));
            $fileinfo && $data['cover'] = '/uploads/' . $fileinfo->getFilename();
        }
        $res = Classes::where('id', $id)->update($data);
        if ($res) return ['status' => 0, 'msg' => '修改成功'];
        return ['status' => 1, 'msg' => '修改失败'];
    }

    /**
     * 删除
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(int $id)
    {
        $res = Classes::destroy($id);
        if ($res) return response()->json(['status' => 0, 'msg' => '成功']);
        return response()->json(['status' => 1, 'msg' => '失败']);
    }
}
